package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"os/exec"
)

func main_() {
	//c := exec.Command("cmd", "/C", "mysqldump -uroot -p654321 -h127.0.0.1 -P3306 db_cow > d:/db_cow.sql")
	var outInfo bytes.Buffer
	c := exec.Command("cmd.exe", "cd D:/PythonProject/BFC_SPIDER/ins-bf-spider-platform/trunk/web_info_by_rule", "D:",
		"scrapy crawl web_info_by_rule -a batch_num=\"1596420880\"")
	//c.Stdin = strings.NewReader("some input")
	c.Stdout = &outInfo
	if err := c.Run(); err != nil {
		fmt.Println("Error: ", err)
	}
	fmt.Println(outInfo.String())
}
func main_bak() {
	cdPath := "cd D:/PythonProject/BFC_SPIDER/ins-bf-spider-platform/trunk/web_info_by_rule"
	d := "D:"
	cmdLine := "scrapy crawl web_info_by_rule -a batch_num=\"1596420880\""
	cmd := exec.Command("cmd.exe", "/c", "start "+cdPath, d)
	b, _ := cmd.Output()
	err := cmd.Run()
	fmt.Printf("%s, error:%v \n", cmdLine, err)
	fmt.Println(string(b))
}

func main_bak2() {
	param := []string{"crawl", "web_info_by_rule", "-a batch_num=\"1596420880\""}
	err := CmdAndChangeDirToShow("D:/PythonProject/BFC_SPIDER/ins-bf-spider-platform/trunk/web_info_by_rule", "scrapy", param)
	fmt.Println(err)
}

// @title 用法一:直接在当前目录使用并返回结果
func Cmd(commandName string, params []string) (string, error) {
	cmd := exec.Command(commandName, params...)
	fmt.Println("Cmd", cmd.Args)
	var out bytes.Buffer
	cmd.Stdout = &out
	cmd.Stderr = os.Stderr
	err := cmd.Start()
	if err != nil {
		return "", err
	}
	err = cmd.Wait()
	return out.String(), err
}

// @title 用法二：在命令位置使用并返回结果
func CmdAndChangeDir(dir string, commandName string, params []string) (string, error) {
	cmd := exec.Command(commandName, params...)
	fmt.Println("CmdAndChangeDir", dir, cmd.Args)
	var out bytes.Buffer
	cmd.Stdout = &out
	cmd.Stderr = os.Stderr
	cmd.Dir = dir
	err := cmd.Start()
	if err != nil {
		return "", err
	}
	err = cmd.Wait()
	return out.String(), err
}

// @title 用法三：在命令位置使用并实时输出每行结果
func CmdAndChangeDirToShow(dir string, commandName string, params []string) error {
	cmd := exec.Command(commandName, params...)
	fmt.Println("CmdAndChangeDirToFile", dir, cmd.Args)
	//StdoutPipe方法返回一个在命令Start后与命令标准输出关联的管道。Wait方法获知命令结束后会关闭这个管道，一般不需要显式的关闭该管道。
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		fmt.Println("cmd.StdoutPipe: ", err)
		return err
	}
	cmd.Stderr = os.Stderr
	cmd.Dir = dir
	err = cmd.Start()
	if err != nil {
		return err
	}
	//创建一个流来读取管道内内容，这里逻辑是通过一行一行的读取的
	reader := bufio.NewReader(stdout)
	//实时循环读取输出流中的一行内容
	for {
		line, err2 := reader.ReadString('\n')
		if err2 != nil || io.EOF == err2 {
			break
		}
		fmt.Println(line)
	}
	err = cmd.Wait()
	return err
}

/*
// @title 用法四：在命令位置使用并实时写入每行结果到文件
func CmdAndChangeDirToFile(fileName, dir, commandName string, params []string) error {
	Path, err := GetCurrentPath()
	if err != nil {
		return err
	}
	Path += "logs/cmdDir/"
	EnsureDir(Path)
	f, err := os.Create(Path + fileName) //创建文件
	defer f.Close()
	cmd := exec.Command(commandName, params...)
	fmt.Println("CmdAndChangeDirToFile", dir, cmd.Args)
	//StdoutPipe方法返回一个在命令Start后与命令标准输出关联的管道。Wait方法获知命令结束后会关闭这个管道，一般不需要显式的关闭该管道。
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		fmt.Println("cmd.StdoutPipe: ", err)
		return err
	}
	cmd.Stderr = os.Stderr
	cmd.Dir = dir
	err = cmd.Start()
	if err != nil {
		return err
	}
	//创建一个流来读取管道内内容，这里逻辑是通过一行一行的读取的
	reader := bufio.NewReader(stdout)
	//实时循环读取输出流中的一行内容
	for {
		line, err2 := reader.ReadString('\n')
		if err2 != nil || io.EOF == err2 {
			break
		}
		_, err = f.WriteString(line) //写入文件(字节数组)
		f.Sync()
	}
	_, err = f.WriteString("=================处理完毕========================") //写入文件(字节数组)
	f.Sync()
	err = cmd.Wait()
	return err
}*/
